package com.kensure.shike.card.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.MapUtils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.card.dao.CardUserDao;
import com.kensure.shike.card.model.CardDefine;
import com.kensure.shike.card.model.CardHuodong;
import com.kensure.shike.card.model.CardUser;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;

/**
 * 用户优惠卷服务实现类
 * 
 * @author fankd created on 2019-6-28
 * @since
 */
@Service
public class CardUserService extends JSBaseService {

	@Resource
	private CardUserDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private CardHuodongService cardHuodongService;
	@Resource
	private CardDefineService cardDefineService;
	@Resource
	private SKUserService sKUserService;

	public CardUser selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<CardUser> selectByIds(Collection<Long> ids) {
		return dao.selectByIds(ids);
	}

	public List<CardUser> selectAll() {
		return dao.selectAll();
	}

	public List<CardUser> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCount() {
		return dao.selectCount();
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(CardUser obj) {
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("card_user"));
		return dao.insert(obj);
	}

	public boolean update(CardUser obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	public boolean delete(Long id) {
		return dao.delete(id);
	}

	public boolean deleteMulti(Collection<Long> ids) {
		return dao.deleteMulti(ids);
	}

	public boolean deleteByWhere(Map<String, Object> parameters) {
		return dao.deleteByWhere(parameters);
	}

	public List<CardUser> selectByUserid(Integer status) {
		SKUser user = sKUserService.getUser();
		Map<String, Object> parameters = MapUtils.genMap("userid", user.getId(),"orderby","jian");
		if(status != null){
			parameters.put("status", status);
		}
		return selectByWhere(parameters);
	}

	/**
	 * 获取可用的卡片
	 * @param userid
	 * @param leiji
	 * @return
	 */
	public List<CardUser> selectCanUse(Long userid,Double leiji) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid,"endMan",leiji,"status",1,"orderby","jian desc");
		return selectByWhere(parameters);
	}
	
	/**
	 * 用户vip增加卡劵
	 * 
	 * @param userid
	 */
	public synchronized void useCard(Long id) {
		CardUser card = selectOne(id);
		if(card.getStatus() != 1){
			BusinessExceptionUtil.threwException("优惠卷已使用");
		}
		card.setStatus(9);
		update(card);
	}
	
	/**
	 * 用户vip增加卡劵
	 * 
	 * @param userid
	 */
	public void newVip(Long userid) {
		List<CardHuodong> huodonglist = cardHuodongService.getVip();
		for (CardHuodong chd : huodonglist) {
			for (int i = 0; i < chd.getNum(); i++) {
				CardDefine card = cardDefineService.selectOne(chd.getCardid());
				CardUser carduser = new CardUser(card, userid);
				insert(carduser);
			}
		}
	}

}
